#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

int main(int argc, char **argv)
{
	int c;

	struct option opts[] = {
		{"help", no_argument, NULL, 'h'}
	};

	while (1) {
		c = getopt_long(argc, argv, "-abc:d::h", opts, NULL);

		if (-1 == c) break;

		switch (c) {
			case 1:
				// getopt第3个参数以'-'开头才能读到
				printf("非选项参数：%s\n", optarg);
				break;
			case 'a':
				printf("A\n");
				break;
			case 'b':
				printf("B\n");
				break;
			case 'c':
				printf("C 参数：%s\n", optarg);
				break;
			case 'd':
				printf("D");
				if (optarg) {
					printf(" 可选参数：%s", optarg);
				}
				printf("\n");
			case 'h':
				printf("帮助文档\n");
				break;
		}
	}

	return 0;
}
